package search;

import com.sun.deploy.util.ArrayUtil;

import java.util.*;

/**
 * （只支持英文）
 *
 * @author yuyufeng
 * @date 2017/10/31
 */
public class ForwardIndex {
    private List<String> docs = new ArrayList<>();
    private Set<String> stopwords = new HashSet<>();


    public ForwardIndex(List<String> list) {
        for (String li : list) {
            li = keywordsHandler(li);
            docs.add(li);
        }
        stopwords.add("is");
        stopwords.add("and");
        stopwords.add("or");
        stopwords.add("the");
        stopwords.add("");

    }


    private String keywordsHandler(String doc) {
        doc = doc.replaceAll(",|:|\\.", " ");
        doc = doc.toLowerCase();
        return doc;
    }

    public Set<String> search(String keywords) {
        keywords = keywordsHandler(keywords);

        //用Set可以去重
        Set<String> result = new HashSet<>();
        String[] kewordsArray = keywords.split(" ");

        for (int i = 0; i < kewordsArray.length; i++) {
            String key = kewordsArray[i];
            for (String doc : docs) {
                List<String> list = Arrays.asList(doc.split(" "));
                if (list.contains(key)) {
                    if (!stopwords.contains(key)) {
                        result.add(doc);
                    }
                }
            }
        }
        return result;
    }

    public static void main(String[] args) {
        List<String> docs = new ArrayList<>();
        String doc = "Inverted index comes from the actual application, it needs to find the record according to the value of the attribute.";
        docs.add(doc);

        doc = "Each item in the index table includes an attribute value and the address of each record with the attribute value";
        docs.add(doc);

        doc = "Attribute values are not determined by records";
        docs.add(doc);

        doc = "Rather, the location of the record is determined by the attribute value";
        docs.add(doc);

        doc = "It is called inverted index";
        docs.add(doc);

        doc = "The data operation is simple: the data used by the search engine is easy to operate, generally speaking, only need to add, delete, change, check several functions";
        docs.add(doc);

        doc = "And data has a specific format, you can design simple and efficient applications for these applications";
        docs.add(doc);

        ForwardIndex forwardIndex = new ForwardIndex(docs);
        Set<String> results = forwardIndex.search("What is Inverted Index.");
        for (String result : results) {
            System.out.println(result);
        }
    }
}
